<?php
/*
 * site/doc-list.phtml
 */

use RHX\Model\Manager\FileManager;
    
    $flagFile  = FileManager::SITE_DOC_FILE ;
    $fileManager->setFlag($flagFile);
    
    $repertory = str_replace('public/', '', $fileManager->getUploadDirectory());    
?>


<?php foreach ($listDoc as $siteDoc):

    ?>
    <div class="doc-elt display2" data-id='<?php echo $siteDoc->id; ?>'>
        
        <span class="doc-name"><?php echo ($siteDoc->libelle != '' ? $siteDoc->libelle : 'Aucun libellé');?></span>
        <input class="doc-name-input"type='text' value='<?php echo $siteDoc->libelle; ?>' <?php echo ($siteDoc->libelle == '' ? "placeholder='Aucun libellé'" : '');?> data-pk='<?php  echo $siteDoc->id; ?>' data-name='libelle' maxlength="30"/> 
        
        <div class="doc-btn">
            <a href ='' class="doc-edit-libelle btn btn-small" title='Editer le libellé' data-pk='<?php  echo $siteDoc->id; ?>' data-name='libelle' style='display:none;'><i class="icon-edit"></i></a>&nbsp;&nbsp;
            <a href ='<?php echo $this->basePath() . '/'.  $repertory . $siteDoc->path ; ?>' class="btn btn-info btn-small" target='_blank'>Voir</a>&nbsp;&nbsp;
            <a class="doc-erase btn btn-danger btn-small" data-pk='<?php echo $siteDoc->id; ?>' data-site='<?php echo $siteDoc->site_id; ?>' >Effacer</a>
        </div>
    </div>
    <?php
endforeach;
?>




<div class="doc-btn-add">
    <button class="btn btn-inverse btn-small" id="doc-close">Fermer</button>    
    
    <?php $flagFile  = FileManager::SITE_DOC_FILE ;
          $fileManager->setFlag($flagFile);
    ?>

    <?php
          $idBtnUplFile= $fileManager->getFlag() . '-upload'; 
          $repertory = str_replace('public/', '', $fileManager->getUploadDirectory());    
    ?>
    
    <!--button class="btn btn-success btn-small" id="doc-add">Ajouter</button-->
    <button class="btn btn-success btn-small" id="<?php echo $idBtnUplFile ; ?>" data-id="<?php echo $site->id; ?>" >Ajouter</button>
</div>

<script type="text/javascript">
    $(function(){
	$('#doc-close').click(function(e){
	    e.preventDefault();
	    $('#site-documents').popover('hide');
	});
        
        $('.doc-erase').on('click', function(){
              var $thisBtn = $(this);
              id_doc = $thisBtn.data('pk');
              id_site = $thisBtn.data('site');
              ajax = $.ajax({
                            type: 'post',
                            url: '<?php echo $this->url('RHX/default', array('controller' => 'site', 'action' => 'eraseDocument' )) ?>',
                            data: 'id='+ id_doc + '&site=' + id_site
                        });
             ajax.done(function(data){
                 if(data.success){
                     $linkDoc = $('#site-documents');                     
                     $closeDoc = $('#doc-close');                     
                     $linkDoc.find('#site-doc-count').html(data.nbCountDoc);
                     
                     $thisBtn.parent().parent().remove();
                 }
                 else{
                     
                 }
             });
             ajax.fail(function(data){
                 
             });
        });
        /*
        $('.doc-edit-libelle').on('click', function(e){
            var $thisBtn = $(this),
                $thisElt =  $thisBtn.parent().parent();
            
            e.preventDefault();
            //$thisElt.removeClass('display1').addClass(/'display2');
            
            $thisElt.find('input.doc-name-input').off().focus().on('blur',function(){
                // saving libelle
                
                pk = $thisBtn.data('pk');
                name = $thisBtn.data('name');
                value = $(this).val();
                        
                ajax = $.ajax({
                            type: 'post',
                            url: '<?php echo $this->url('RHX/default', array('controller' => 'site', 'action' => 'updateDocument' )) ?>',
                            data: 'pk='+ pk + '&name=' + name+ '&value='+value
                        });
                //update dom
                ajax.done(function(data){
                    $thisElt.find('.doc-name').html(value);
                    //$thisElt.removeClass('display2').addClass('display1');
                });
            });
        });*/
    
        $('.doc-name-input').on('blur',function(){
                // saving libelle
                $thisBtn = $(this);
                $thisElt =  $thisBtn.parent().parent();
                
                pk = $thisBtn.data('pk');
                name = $thisBtn.data('name');
                value = $thisBtn.val();
                        
                ajax = $.ajax({
                            type: 'post',
                            url: '<?php echo $this->url('RHX/default', array('controller' => 'site', 'action' => 'updateDocument' )) ?>',
                            data: 'pk='+ pk + '&name=' + name+ '&value='+value
                        });
                //update dom
                ajax.done(function(data){
                    $thisElt.find('.doc-name').html(value);
                    //$thisElt.removeClass('display2').addClass('display1');
                });
            });
        $('.doc-name-input').on('change', function(){
            console.log($(this).val());
            });
    });
</script>

<script type='text/javascript'>
         <?php $flag  = FileManager::SITE_DOC_FILE; ?>    

         $("#<?php echo $flagFile;?>-upload").on('click', function(e){
            <?php $sPattern = '__0' ;?>    

            var url_motif = '<?php echo $this->url('RHX/default', array('controller'	 => 'file', 'action'	 => 'upload', 'id' => $sPattern)) ;?>?flag=<?php echo $flagFile ;?>',
                $this = $(this);

            e.preventDefault();
            url = url_motif.replace('<?php  echo $sPattern ;?>', $this.data('id')) ;                        
            uploadWindow = window.open(url, 'UploadWindow', 'menubar=no, scrollbars=no, top=100, left=100, width=500, height=400');		
        });
        
</script>


<style type="text/css">
    .popover{
	width: 450px;
    }
    .doc-elt{
	width: 100%;
	height: 2em;
	line-height: 2em;
	margin-bottom: 3px;
	padding: 3px;
	border: solid 1px #eeeeee;
	background: #ffffff;
    }
    .doc-elt:hover{
	box-shadow: 0px 0px 1px 1px #dddddd;
    }
    .doc-btn{
	float: right;
    }
    
    .doc-elt.display1 .doc-name{
	display: '';
        height: 25px;
        padding-left: 6px;
        width: 280px;
    }
    .doc-elt.display1 .doc-name-input{
	display: none; 
    }
    
    .doc-elt.display2 .doc-name{
	display: none;
    }
    .doc-elt.display2 .doc-name-input{
	display: '';
        height: 25px;
        padding-left: 6px;
        width: 280px;
    }
    
    .doc-elt.display2 .doc-name-input:hover{
        background-color: #FFFFFF;
        border: 1px solid #CCCCCC;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset;
        transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s;
    }
    .doc-btn-add{
	margin: 5px auto;
	float: right;
    }
</style>

